<?php

/*
=====================================================
 FluxBB - Delux Board Converstations (DBC)
-----------------------------------------------------
 http://dbc-forum.com/
 http://fluxbb.org/
-----------------------------------------------------
 Copyright (c) 2010,2011 FluxBB - Delux Board Conversations (DBC)
-----------------------------------------------------
 Предсловие: Данный скрипт (DBC) не является работой с нуля, то 
 есть ядром скрипта всё также остаётся FuxBB,но теперь в оболочке
 DBC.
=====================================================
*/

define('DBC_ROOT', './');
require DBC_ROOT.'engine/common.php';

preg_match_all('#<DBC_include "([^/\\\\]*?)\.(php[45]?|inc|html?|txt)">#', $tpl_main, $DBC_includes, PREG_SET_ORDER);

if ($DBC_user['g_read_board'] == '0')
	message($lang_common['No view']);


// Load the index.php language file
require DBC_ROOT.'language/'.$DBC_user['language'].'/index.php';

// Get list of forums and topics with new posts since last visit
if (!$DBC_user['is_guest'])
{
	$result = $db->query('SELECT t.forum_id, t.id, t.last_post FROM '.$db->prefix.'topics AS t INNER JOIN '.$db->prefix.'forums AS f ON f.id=t.forum_id LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=f.id AND fp.group_id='.$DBC_user['g_id'].') WHERE (fp.read_forum IS NULL OR fp.read_forum=1) AND t.last_post>'.$DBC_user['last_visit'].' AND t.moved_to IS NULL') or error('Unable to fetch new topics', __FILE__, __LINE__, $db->error());


	$new_topics = array();
	while ($cur_topic = $db->fetch_assoc($result))
		$new_topics[$cur_topic['forum_id']][$cur_topic['id']] = $cur_topic['last_post'];

	$tracked_topics = get_tracked_topics();

}

if ($DBC_config['o_feed_type'] == '1')
	$page_head = array('feed' => '<link rel="alternate" type="application/rss+xml" href="extern.php?action=feed&amp;type=rss" title="'.$lang_common['RSS active topics feed'].'" />');
else if ($DBC_config['o_feed_type'] == '2')
	$page_head = array('feed' => '<link rel="alternate" type="application/atom+xml" href="extern.php?action=feed&amp;type=atom" title="'.$lang_common['Atom active topics feed'].'" />');

$page_title = array(DBC_htmlspecialchars($DBC_config['o_board_title']));
define('DBC_ALLOW_INDEX', 1);
define('DBC_ACTIVE_PAGE', 'index');
require DBC_ROOT.'engine/engine.php';
$show = '5'; // Change 5 by the number of topics you want the mod to display
require DBC_ROOT.'engine/last_topics.php';


// Print the categories and forums
$result = $db->query('SELECT u.group_id, u.id AS uid, c.id AS cid, c.cat_name, f.id AS fid, f.forum_name, f.forum_desc, f.redirect_url, f.moderators, f.num_topics, f.num_posts, f.last_post, f.last_post_id, f.id, f.last_poster, f.last_poster_id, t.subject FROM '.$db->prefix.'categories AS c INNER JOIN '.$db->prefix.'forums AS f ON c.id=f.cat_id
LEFT JOIN '.$db->prefix.'topics AS t ON f.last_post_id=t.last_post_id 
LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=f.id AND fp.group_id='.$DBC_user['g_id'].') LEFT JOIN '.$db->prefix.'users AS u ON (f.last_poster=u.username  AND f.last_poster_id=u.id) WHERE (fp.read_forum IS NULL OR fp.read_forum=1)
 ORDER BY c.disp_position, c.id, f.disp_position DESC', true) or error('Unable to fetch category/forum list', __FILE__, __LINE__, $db->error());

$tpl_main = str_replace('{cat_name}', DBC_htmlspecialchars($cur_forum['cat_name']), $tpl_main);
$tpl_main = str_replace('{collapse}', (($div_box_old) ? '' : '<span class="conr"><img src="'.$exp_img.'" onclick="togglecategory('.$cat_count.')" alt="Collapse" id="img_'.$cat_count.'" /></span>'), $tpl_main);


// Mod collapse
$cat_ids = (isset($_COOKIE['collapseprefs'])) ? explode(',', $_COOKIE['collapseprefs']) : array();
$cur_category = 0;
$cat_count = 0;
$forum_count = 0;
$subforums_array = array();
while ($cur_forum = $db->fetch_assoc($result))
{
    if ($cur_forum['parent_forum_id'] != 0) {
        if (!array_key_exists($cur_forum['parent_forum_id'], $subforums_array)) {
            $subforums_array[$cur_forum['parent_forum_id']] = array();
            }
        $subforums_array[$cur_forum['parent_forum_id']][] = '<a href="forum_'.$cur_forum['fid'].'">'.$cur_forum['forum_name'].'</a>';
    } else {
	$moderators = '';

	if ($cur_forum['cid'] != $cur_category) // A new category since last iteration?
	{
		if ($cur_category != 0)
			echo "\t\t\t".'</tbody>'."\n\t\t\t".'</table>'."\n\t\t".'</div>'."\n\t".'</div>'."\n".'</div>
<table width="100%" height="23" border="0" cellpadding="0" cellspacing="0"><thead><tr>
<td align="left" width="30" class="bg_forum_bott_left"><img src="engine/skins/images/spacer.gif" border="0" alt="" /></td>
<td align="left" class="bg_forum_bott_center"><img src="engine/skins/images/spacer.gif" border="0" alt="" /></td>
<td align="center" width="30" class="bg_forum_bott_right"><img src="engine/skins/images/spacer.gif" border="0" alt="" /></td>
</tr></thead></table>
'."\n\n";

		++$cat_count;
		$forum_count = 0;

		// Mod collapse - Visman
		$div_box = in_array($cat_count, $cat_ids) ? ' style="display:none"' : '';
		$exp_img_name = strpos($div_box,'none') !== false ? 'exp_down.png' : 'exp_up.png';
		$exp_img = (is_file('engine/skins/images/exp_down.png') ? 'engine/skins/images/' : 'img/') . $exp_img_name;
		if (file_exists(DBC_ROOT.'style/'.$DBC_user['style'].'/old.php'))
			$div_box_old = true;
		else
			$div_box_old = false;

?>

<table width="100%" height="38" border="0" cellpadding="0" cellspacing="0"><thead><tr>
<td align="left" width="28" class="bg_forum_title_left"><img src="engine/skins/images/spacer.gif" border="0" alt="" /></td>
<td align="left" class="bg_forum_title_center"><?php echo (($div_box_old) ? '<span class="conr"><img src="'.$exp_img.'" onclick="togglecategory('.$cat_count.')" alt="Collapse" id="img_'.$cat_count.'" /></span>' : '') ?><span class="cat_title"><?php echo DBC_htmlspecialchars($cur_forum['cat_name']) ?></span></td>
<td align="center" width="28" class="bg_forum_title_right"><?php echo (($div_box_old) ? '' : '<span class="conr Collapse_hover"><img src="'.$exp_img.'" onclick="togglecategory('.$cat_count.')" alt="Collapse" id="img_'.$cat_count.'" /></span>') ?></td>
</tr></table>


			<table height="17" style="height: 17px;" cellpadding="0" cellspacing="0" border="1" class="bg_forum_r cClass blocktable">
			<thead>
				<tr>
					<td class="tcl tcr_t" scope="col"><span style="font-size:11px;"><?php echo $lang_common['Forum_n'] ?></span></td>
					<td class="tc2 tcr_t" scope="col"><span style="font-size:11px;"><?php echo $lang_index['Topics'] ?></span></td>
					<td class="tc3 tcr_t" scope="col"><span style="font-size:11px;"><?php echo $lang_common['Posts'] ?></span></td>
					<td class="tcr tcr_t" scope="col"><span style="font-size:11px;"><?php echo $lang_common['Last post'] ?></span></td>
				</tr>
			</thead>
			</table>


<div id="idx<?php echo $cat_count ?>">
	<div class="box"<?php echo(($div_box_old) ? ' id="box_'.$cat_count.'"'.$div_box : '') ?>>
		<div class="inbox">

			<table cellspacing="0" class="block_table_b cClass blocktable bg_forum_td" border="">
			<tbody<?php echo(($div_box_old) ? '' : ' id="box_'.$cat_count.'"'.$div_box) ?>>
<?php

		$cur_category = $cur_forum['cid'];
	}

	++$forum_count;
	$item_status = ($forum_count % 2 == 0) ? 'roweven' : 'rowodd';
	$forum_field_new = '';
	$icon_type = 'icon';

	// Are there new posts since our last visit?
	if (!$DBC_user['is_guest'] && $cur_forum['last_post'] > $DBC_user['last_visit'] && (empty($tracked_topics['forums'][$cur_forum['fid']]) || $cur_forum['last_post'] > $tracked_topics['forums'][$cur_forum['fid']]))
	{
		// There are new posts in this forum, but have we read all of them already?
		foreach ($new_topics[$cur_forum['fid']] as $check_topic_id => $check_last_post)
		{
			if ((empty($tracked_topics['topics'][$check_topic_id]) || $tracked_topics['topics'][$check_topic_id] < $check_last_post) && (empty($tracked_topics['forums'][$cur_forum['fid']]) || $tracked_topics['forums'][$cur_forum['fid']] < $check_last_post))
			{
				$item_status .= ' inew';
				$forum_field_new = '<span class="newtext">[ <a href="search.php?action=show_new&amp;fid='.$cur_forum['fid'].'">'.$lang_common['New posts'].'</a> ]</span>';
				$icon_type = 'icon icon-new';

				break;
			}
		}
	}

	// Is this a redirect forum?
	if ($cur_forum['redirect_url'] != '')
	{
		$forum_field = '<h3><span class="redirtext">'.$lang_index['Link to'].'</span> <a href="'.DBC_htmlspecialchars($cur_forum['redirect_url']).'" title="'.$lang_index['Link to'].' '.DBC_htmlspecialchars($cur_forum['redirect_url']).'">'.DBC_htmlspecialchars($cur_forum['forum_name']).'</a></h3>';
		$num_topics = $num_posts = '-';
		$item_status .= ' iredirect';
		$icon_type = 'icon';
	}
	else
	{
		$forum_field = '<h3><a href="forum_'.$cur_forum['fid'].'">'.DBC_htmlspecialchars($cur_forum['forum_name']).'</a>'.(!empty($forum_field_new) ? ' '.$forum_field_new : '').'</h3>';
		$num_topics = $cur_forum['num_topics'];
		$num_posts = $cur_forum['num_posts'];
	}

	if ($cur_forum['forum_desc'] != '')
		$forum_field .= "\n\t\t\t\t\t\t\t\t".'<div class="forumdesc"><span class="forumdesc_t">'.$cur_forum['forum_desc'].'</span></div>';

	// If there is a last_post/last_poster
	if ($cur_forum['last_post'] != '')
	    $last_post = ''.$lang_common['Topic'].': <span style="padding: 0px 3px 0px 0px;"><a href="user_'.DBC_htmlspecialchars($cur_forum['last_poster_id']).'" title="'.$lang_common['Profile'].' '.DBC_htmlspecialchars($cur_forum['last_poster']).'">'.generate_avatar_markup($cur_forum['last_poster_id']).'</a></span><a href="topics_'.$cur_forum['last_post_id'].'#p'.$cur_forum['last_post_id'].'">'.DBC_htmlspecialchars($cur_forum['subject']).'</a> <span class="byuser">'.$lang_common['Author'].': <a href="user_'.DBC_htmlspecialchars($cur_forum['last_poster_id']).'" title="'.$lang_common['Profile'].' '.DBC_htmlspecialchars($cur_forum['last_poster']).'">'.DBC_htmlspecialchars($cur_forum['last_poster']).'</a> (<i style="font-size: 11px;">'.format_time($cur_forum['last_post']).'</i>)</span>';
	else if ($cur_forum['redirect_url'] != '')
		$last_post = '- - -';
	else
		$last_post = $lang_common['Never'];

	if ($cur_forum['last_post'] != '')
	{
		if (isset($cur_forum['group_id'])) // user
			$col_group = colorize_group($cur_forum['last_poster'], $cur_forum['group_id'], $cur_forum['uid']);
		else // guest
			$col_group = colorize_group($cur_forum['last_poster'], DBC_GUEST);

		$last_post = str_replace('<span class="byuser">'.$lang_common['by'].' '.DBC_htmlspecialchars($cur_forum['last_poster']).'</span>', '<span class="byuser">'.$lang_common['by'].' '.$col_group.'</span>', $last_post);
	}

	if ($cur_forum['moderators'] != '')
	{
		$mods_array = unserialize($cur_forum['moderators']);
		$moderator_groups = array();
		if (isset($mods_array['groups']))
		{
			$moderator_groups = $mods_array['groups'];
			unset($mods_array['groups']);
		}

		if (count($mods_array) > 0)
		{
			$moderators = array();

			foreach ($mods_array as $mod_username => $mod_id)
			{
				if (isset($moderator_groups[$mod_id]))
					$moderators[] = colorize_group($mod_username, $moderator_groups[$mod_id], $mod_id);
				elseif ($DBC_user['g_view_users'] == '1')
					$moderators[] = '<a href="user_'.$mod_id.'">'.DBC_htmlspecialchars($mod_username).'</a>';
				else
					$moderators[] = DBC_htmlspecialchars($mod_username);
			}

			$moderators = "\t\t\t\t\t\t\t\t".'<span class="modlist">(<em>'.$lang_common['Moders'].'</em> '.implode(', ', $moderators).')</span>'."\n";
		}
if (array_key_exists($cur_forum['fid'], $subforums_array))
    {
        $subforums = "\t\t\t\t\t\t\t\t".'<p>('.$lang_common['Subforums'].' '.implode(', ', $subforums_array[$cur_forum['fid']]).')</p>'."\n";
    } else $subforums = '';

	}

?>
<tr class="<?php echo $item_status ?> bg_forum_td">
					<td class="tcl">
						<div class="<?php echo $icon_type ?>"><div class="nosize"><?php echo forum_number_format($forum_count) ?></div></div>
						<div class="tclcon">
							<div>
								<?php echo $forum_field."\n".$moderators."\n".$subforums ?>
							</div>
						</div>
					</td>
					<td class="tc2"><span class="postps_num"><?php echo forum_number_format($num_topics) ?></span></td>
					<td class="tc3"><span class="postps_num"><?php echo forum_number_format($num_posts) ?></span></td>
					<td class="tcr"><div class="last_post_img"><?php echo $last_post ?></div></td>
				</tr>

<?php
}
}
// Did we output any categories and forums?
if ($cur_category > 0)
	echo "\t\t\t".'</tbody>'."\n\t\t\t".'</table>

<table width="100%" height="23" border="0" cellpadding="0" cellspacing="0"><thead><tr>
<td align="left" width="30" class="bg_forum_bott_left"><img src="engine/skins/images/spacer.gif" border="0" alt="" /></td>
<td align="left" class="bg_forum_bott_center"><img src="engine/skins/images/spacer.gif" border="0" alt="" /></td>
<td align="center" width="30" class="bg_forum_bott_right"><img src="engine/skins/images/spacer.gif" border="0" alt="" /></td>
</tr></thead></table>

'."\n\t\t".'</div>'."\n\t".'</div>'."\n".'</div>'."\n\n";
else
	echo '<div class="annn" style="margin-bottom: 16px;">
			<div class="ann">'.$lang_index['Empty board'].'</div>
		</div>';
?>
<?php
require DBC_ROOT.'engine/dbc_stats.php';
?>

<?php

$footer_style = 'index';
require DBC_ROOT.'engine/footer.php';
?>